﻿using System;
using UnityEditor;
using UnityEngine;

namespace XUBuilder
{
    namespace Editor
    {
        internal class XUBuilderWindow : EditorWindow
        {
            [MenuItem("XUPK/XUBuilder/Configure", false, 1)]
            public static void SetConfig()
            {
                EditorWindow window = GetWindow<XUBuilderWindow>("Builder");
                window.minSize = window.maxSize = new Vector2(800, 800);
                window.Show();
            }

            private enum ToolbarEnum
            {
                Standalone,
                Android,
                iOS,
            }
            private ToolbarEnum toolbar = ToolbarEnum.Android;

            private XUBuilderStandaloneSetting standaloneSetting = null;
            private XUBuilderAndroidSetting androidSetting = null;
            private XUBuilderiOSSetting iOSSetting = null;

            private void OnEnable()
            {
                if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.StandaloneWindows)
                {
                    toolbar = ToolbarEnum.Standalone;
                }
                if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android)
                {
                    toolbar = ToolbarEnum.Android;
                }
                if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.iOS)
                {
                    toolbar = ToolbarEnum.iOS;
                }
            }

            private void OnGUI()
            {
                GUILayout.BeginHorizontal();
                GUILayout.Space(50);
                toolbar = (ToolbarEnum)GUILayout.Toolbar((int)toolbar, Enum.GetNames(typeof(ToolbarEnum)), GUILayout.Height(30));
                GUILayout.Space(50);
                GUILayout.EndHorizontal();

                GUIStyle centeredStyle = new GUIStyle(GUI.skin.GetStyle("Label"));
                centeredStyle.alignment = TextAnchor.UpperCenter;
                centeredStyle.fontSize = 16;

                switch (toolbar)
                {
                    case ToolbarEnum.Standalone:
                        GUILayout.Label(new GUIContent("Standalone"), centeredStyle);
                        if (standaloneSetting == null)
                        {
                            standaloneSetting = XUBuilderStandaloneSetting.LoadOrCreate();
                        }
                        standaloneSetting.OnGUI(this);
                        standaloneSetting.Save();
                        break;
                    case ToolbarEnum.Android:
                        GUILayout.Label(new GUIContent("Android"), centeredStyle);
                        if (androidSetting == null)
                        {
                            androidSetting = XUBuilderAndroidSetting.LoadOrCreate();
                        }
                        androidSetting.OnGUI(this);
                        androidSetting.Save();
                        break;
                    case ToolbarEnum.iOS:
                        GUILayout.Label(new GUIContent("iOS"), centeredStyle);
                        if (iOSSetting == null)
                        {
                            iOSSetting = XUBuilderiOSSetting.LoadOrCreate();
                        }
                        iOSSetting.OnGUI(this);
                        iOSSetting.Save();
                        break;
                }
            }
        }
    }
}